package com.supcalls.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CallableExample {
    public static void main(String[] args) {
        // 创建线程池
        ExecutorService executorService = Executors.newCachedThreadPool();

        // 创建一个 Callable 任务
        Callable<String> task = () -> {
            // 模拟任务执行
            Thread.sleep(1000);
            return "Task completed!";
        };

        // 提交任务并获得 Future 对象
        Future<String> future = executorService.submit(task);

        try {
            // 获取任务执行的返回值
            String result = future.get();
            System.out.println("Task result: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        // 关闭线程池
        executorService.shutdown();
    }
}

